

local dir `c(pwd)'
use entropy_bible, clear
merge m:1 trans using data_bible_final, keep(3)


keep if book==40|book==41|book==42|book==43|book==44|book==66

/*
75% test: drop the lower 10% for chars
and then truncate the remaining 90% at p10.
this ensures a constant sample size
*/

local books "40 41 42 43 44 66"

generate samplesize_chars=.
	foreach book of local books {
		sum chars_original if book==`book', d
		local minimum=r(p10)
		replace samplesize_chars=`minimum' if book==`book' 
		drop if chars_original<`minimum' & book==`book'
	}
		
keep book trans language latitude longitude family ISO sample*
gen random=runiform()

egen group = cut(random), group(`1') label

replace group=group+1	
sum group
local min=r(min)
local max=r(max)

forvalues g=`min'/`max' {
	preserve
	keep if group==`g'
	save "`dir'\\calculation_`g'", replace
	winexec C:\Program Files (x86)\Stata14\StataMP-64 -q do `dir'\\entropy_calculation_validation_VII `g'
	restore
}

/* wait until everything is finished */
clear
 forvalues g=1/`1'{  
   capture confirm file finished_`g'.dta
   while _rc != 0 {
      sleep 2000
      capture confirm file finished_`g'.dta
   }
 }
use  entropy_bible_1, clear 
drop in 1/l
forvalues g=1/`1' {
	capture append using entropy_bible_`g'
	capture erase entropy_bible_`g'.dta
	capture erase calculation_`g'.dta
	capture erase finished_`g'.dta
}
drop if entropy_original==.

/* calc D values */
foreach type in original order structure {
gen H_`type'=(entropy_`type'/chars_`type')^(-1)

} 

drop if H_original==.

generate D_structure=H_structure-H_original
generate D_order=H_order-H_original


save entropy_bible_validation_VII, replace 


   	
exit